﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Windows.Forms;

namespace Rail.Commons.SendMail
{
    public class Send163Mail
    {
        public static void StartSend163Mail(string mailBody)
        {
            string str = "true";
            if (str == ConfigurationManager.AppSettings["MailException"])
            {

                //实例化两个必要的
                MailMessage mail = new MailMessage();
                SmtpClient smtp = new SmtpClient();

                //发送邮箱地址
                mail.From = new MailAddress("bb15829695398@163.com");

                //收件人(可以群发)
                mail.To.Add(new MailAddress("18555112885@163.com"));

                //是否以HTML格式发送
                mail.IsBodyHtml = true;
                //主题的编码格式
                mail.SubjectEncoding = Encoding.UTF8;
                //邮件的标题
                //mail.Subject = "测试一下发件的标题";
                mail.Subject = "系统异常邮件提醒，计算机名称:"+ Environment.UserName +"，IP地址:...";
                //内容的编码格式
                mail.BodyEncoding = Encoding.UTF8;
                //邮件的优先级
                mail.Priority = MailPriority.Normal;
                //发送内容,带一个图片标签,用于对方打开之后,回发你填写的地址信息
                //mail.Body = @"获取打开邮件的用户IP，图片由服务器自动生成：<img src='" + Receipt + "'>";
                mail.Body = mailBody;

                //发件邮箱的服务器地址
                smtp.Host = "smtp.163.com";
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Timeout = 1500;
                //是否为SSL加密
                smtp.EnableSsl = true;
                //设置端口,如果不设置的话,默认端口为25
                smtp.Port = 25;
                smtp.UseDefaultCredentials = true;
                //验证发件人的凭据
                smtp.Credentials = new System.Net.NetworkCredential("bb15829695398@163.com", "BAIBIN19920807");
                try
                {
                    //发送邮件
                    smtp.Send(mail);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
            }
        }
    }
}